package com.example.materialviewpagerdemo.view;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.materialviewpagerdemo.R;

public class PopRecyclerviewAdapter extends RecyclerView.Adapter <PopRecyclerviewAdapter.MyViewHolder>{

    Context context;
    View.OnClickListener onClickListener;
    private int anInt=10;
    private String[] dataTypeName;
    private RecyclerViewListener recyclerViewListener;

    //此处可以自行设置监听
    public interface RecyclerViewListener
    {
        void onItemClick(View view,int position);
    }


    public PopRecyclerviewAdapter(Context context, String[] dataTypeName) {
        this.context = context;
        this.dataTypeName = dataTypeName;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder myViewHolder=new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.recyleritem, parent, false));
        return myViewHolder;
    }


    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.textView.setText(dataTypeName[position]);

        //设置监听
        if (recyclerViewListener!=null)
        {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position=holder.getLayoutPosition();
                    recyclerViewListener.onItemClick(holder.textView,position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return dataTypeName.length;
    }

    class MyViewHolder extends RecyclerView.ViewHolder
    {
        TextView textView;
        View.OnClickListener onClickListener;

        public MyViewHolder(View itemView) {
            super(itemView);
            textView=itemView.findViewById(R.id.recycleritem);
        }
    }

//    public void setMyOnClickListener(View.OnClickListener onClickListener) {
//        this.onClickListener = onClickListener;
//    }

    public void setRecyclerViewListener(RecyclerViewListener recyclerViewListener)
    {
        this.recyclerViewListener=recyclerViewListener;
    }
}
